import {reloadVM} from "./out.js";

import {Counter, Gauge, Histogram, Summary} from 'prom-client'


export const reloadCounter = new Counter({ name: 'reload_counter', help: 'metric_help' });
export const bookingTime = new Histogram({
    name: 'meta_booking_time',
    help: 'metric_help',
    buckets: [0.1, 5, 15, 50, 100, 500],
});

let traceChannelsCountMap = new Map();

// ota数量小于15且不包含ly.com的数量
let invalidCount = 0;

let requestCount = 0;

export function incrRequestCount(){
    requestCount++;
}
/**
 * 统计ota数量问题
 * @param traceId
 * @param {Array} channels
 */
export function addTraceChannels(traceId, channels) {
    traceChannelsCountMap.set(traceId, channels.length)
    if (channels.length < 15 && !channels.includes('Trip.com') && !channels.includes('ly.com')) {
        // invalidGauge.inc()
        invalidCount++;
    }

}

/**
 * 校验有效率
 */
export function verifiyEfficiency(log) {
    let precent = 0.00
    if (requestCount != 0){
        precent = (((requestCount - invalidCount) / requestCount) * 100).toFixed(2)
        if (invalidCount / requestCount > 0.05){
            log.warn('reloadVMMM')
            reloadVM();
        }
    }
    log.warn(`invalid: ${invalidCount} total: ${requestCount} success.percent: ${precent}%`)

}

export function resetCount(){
    requestCount = 0;
    invalidCount = 0;
}

export function addReloadCounter(){
    reloadCounter.inc(1);
}